/**
 * 
 */
package view.impl;

import java.io.FileNotFoundException;

import model.EMenuState;
import model.ModelFactory;
import model.impl.menu.IMainMenu;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;

import util.ResLoc;
import view.impl.menu.SingleLayeredMenu;
import view.impl.menu.items.MenuButton;
import view.impl.menu.items.MenuCheckBox;
import view.impl.menu.items.MenuTextBox;


/**
 * A class that implements the interface IViewState. Defines a state that displays
 * the settings menu. Contains controls for server name, nickname and
 * fullscreen/windowed.
 * 
 * @author Julian Lindblad
 */
public class SettingsMenuState implements IViewState {
	
	//private static final SettingsMenuState INSTANCE = new SettingsMenuState();
	
	private UnicodeFont uFont = null;
	private UnicodeFont userTextFont = null;
	
	private Image background;
	
	private SingleLayeredMenu menu;
	private MenuTextBox serverIPTextBox;
	private MenuCheckBox fullScreenCheckBox;
	private MenuTextBox nicknameTextBox;
	private MenuButton backButton;
	
	private int windowWidth = 800;
	private int windowHeight = 600;
	
//	/**
//	 * @return A singleton instance of SettingsMenuState.
//	 */
//	static IViewState getInstance() { 
//		return INSTANCE;
//	}
	
	/**
	 * Private constructor, as defined in Singleton-pattern.
	 */
	SettingsMenuState() throws RenderException {
		
		try {
			uFont = FontContainer.getMenuFont();
			userTextFont = FontContainer.getUserTextFont();
		} catch (SlickException e) {
			throw new RenderException("Unable to load fonts in Settings menu", e);
		} catch (FileNotFoundException e) {
			throw new RenderException("Unable to load fonts in Settings menu", e);
		}
		
		
		// Initiate background image.
		try {
			background = new Image(ResLoc.getResource("Settings.background"));
		} catch (SlickException e) {
			throw new RenderException("Unable to load background for settings screen.", e);
		} catch (FileNotFoundException e) {
			throw new RenderException("Unable to load background for settings screen.", e);
		}
		
		// Initiate menu.
		menu = new SingleLayeredMenu();
		
		serverIPTextBox = new MenuTextBox("Server URL:", "", uFont, userTextFont, 15);
		serverIPTextBox.setPos((windowWidth - serverIPTextBox.getWidth()) / 2, (int) (windowHeight * 0.20));
		serverIPTextBox.setUserText("sdfsd");
		
		nicknameTextBox = new MenuTextBox("Nickname:", "", uFont, userTextFont, 10);
		nicknameTextBox.setPos((windowWidth - nicknameTextBox.getWidth()) / 2, (int) (windowHeight * 0.4));
		nicknameTextBox.setUserText("sdfsd");
		
		fullScreenCheckBox = new MenuCheckBox("Fullscreen: ", "", uFont);
		fullScreenCheckBox.setPos((windowWidth - fullScreenCheckBox.getWidth()) / 2, (int) (windowHeight * 0.6));
		fullScreenCheckBox.setChecked(false);
		
		backButton = new MenuButton("Back", uFont, (windowWidth - uFont.getWidth("Back")) / 2, (int) (windowHeight * 0.8));
		
		menu.addMenuItem(serverIPTextBox, 0);
		menu.addMenuItem(nicknameTextBox, 1);
		menu.addMenuItem(fullScreenCheckBox, 2);
		menu.addMenuItem(backButton, 3);
	}
	
	/* (non-Javadoc)
	 * @see client.view.IViewState#changeResolution(int, int)
	 */
	@Override
	public void changeResolution(int targetWidth, int targetHeight) {
		windowWidth = 800;
		windowHeight = 600;
	}

	/* (non-Javadoc)
	 * @see client.view.IViewState#render(client.view.RenderParameters)
	 */
	@Override
	public void render(Graphics g) {
		background.draw(0, 0, windowWidth, windowHeight);
		
		IMainMenu model = ModelFactory.getViewModel().getMenuState();
		serverIPTextBox.setUserText(model.getIP());
		nicknameTextBox.setUserText(model.getNick());
		fullScreenCheckBox.setChecked(model.isFullScreen());
		
		menu.draw(model.getMarkedAlternative(), g, windowWidth, windowHeight);
	}

	@Override
	public EMenuState getEMenuTyoe() {
		return EMenuState.SETTINGS;
	}
}
